package com.bjgov.scoresys.util;

import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import com.bjgov.scoresys.entity.Project;

public class RequestUtil {
	public static double getDoubleParameter(HttpServletRequest request,
			String name, double defaultValue) {
		double val = defaultValue;
		try {
			val = Double.parseDouble(request.getParameter(name));
		} catch (Exception e) {
			val = defaultValue;
		}
		return val;
	}

	public static float getFloatParameter(HttpServletRequest request,
			String name, float defaultValue) {
		float val = defaultValue;
		try {
			val = Float.parseFloat(request.getParameter(name));
		} catch (Exception e) {
			val = defaultValue;
		}
		return val;
	}

	public static int getIntParameter(HttpServletRequest request, String name,
			int defaultValue) {
		int intValue = defaultValue;
		try {
			intValue = Integer.parseInt(request.getParameter(name));
		} catch (Exception e) {
			intValue = defaultValue;
		}
		return intValue;
	}

	public static String getRemoteAddr(HttpServletRequest request,
			String defaultValue) {
		return request.getRemoteAddr();
	}

	public static String getRequestURL(HttpServletRequest request,
			String defaultValue) {
		return request.getRequestURL().toString();
	}

	public static long getLongParameter(HttpServletRequest request,
			String name, int defaultValue) {
		long longValue = defaultValue;
		try {
			Long tmpInteger = new Long(request.getParameter(name));
			longValue = tmpInteger.longValue();
		} catch (Exception e) {
			longValue = defaultValue;
		}
		return longValue;
	} // function

	public static String getStringParameter(HttpServletRequest request,
			String name, String defaultValue) {
		String strValue = request.getParameter(name);
		if (strValue == null) {
			strValue = "";
		}
		if (strValue.trim().equals(""))
			strValue = defaultValue;
		return strValue;
	}
	
	public static String getStringAttribute(HttpServletRequest request,
			String name, String defaultValue) {
		String strValue = (String) request.getAttribute(name);
		if (strValue == null) {
			strValue = "";
		}
		if (strValue.trim().equals(""))
			strValue = defaultValue;
		return strValue;
	}	
	
	
	public static List<Project> getObjectListAttribute(HttpServletRequest request,
			String name, String defaultValue) {
		List<Project> o =  (List)request.getAttribute(name);
		if (o == null) {
			o = null;
		}
		//if (strValue.trim().equals(""))
			//strValue = defaultValue;
		return o;
	}	
	
	
	public static Object getObjectAttribute(HttpServletRequest request,
			String name, String defaultValue) {
		Object o =  (Object)request.getAttribute(name);
		if (o == null) {
			o = null;
		}
		//if (strValue.trim().equals(""))
			//strValue = defaultValue;
		return o;
	}	

	public static String parseParameter(String str) {
		if (str == null)
			return "";
		else
			return str.trim();
	}

	public static String replaceSpace(String strOriginal) {
		StringBuffer sbuf = new StringBuffer();
		char ch = '\u3000';

		StringTokenizer st = new StringTokenizer(strOriginal, "\n");
		while (st.hasMoreTokens()) {
			String str = st.nextToken();
			StringBuffer tempBuf = new StringBuffer();
			for (int i = 0; i < str.length(); i++) {
				char tempChar = str.charAt(i);
				if (tempChar == ch) {
					tempBuf.append(" ");
				} else {
					tempBuf.append(tempChar);
				}
			}
			str = tempBuf.toString();
			sbuf.append("    ").append(str.trim()).append("\n");
		}
		return sbuf.toString();
	}
}